package disruptor;

import netty.util.StartAndStop;
import org.jctools.queues.MpscBlockingConsumerArrayQueue;
import test.PerformanceAnalyze;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

public class MpscQueueTest extends StartAndStop {

    MpscBlockingConsumerArrayQueue<Long> queue = new MpscBlockingConsumerArrayQueue<>(10);

    Executor executor = Executors.newFixedThreadPool(2);

    PerformanceAnalyze performanceAnalyze = new PerformanceAnalyze();

    public void start() throws InterruptedException {
        executor.execute(() -> {
            try {
                while (running) {
                    Long time = queue.take();

                    performanceAnalyze.delay(System.nanoTime() - time);
                    performanceAnalyze.meter();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
//        executor.execute(() -> {
//            try {
//                while (running) {
//                    Long time = queue.take();
//
//                    performanceAnalyze.update(System.nanoTime() - time);
//                    performanceAnalyze.meter();
//                }
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//        });

        while (running) {
            queue.offer(System.nanoTime());
        }
    }

    public static void main(String[] args) throws InterruptedException {
        new MpscQueueTest().start();
    }

}
